%% process_showint
function process_showint(int_type)
[points,~] = integral_ref(int_type);
switch int_type
    case "1d"
        V = [-1; 1];
        T = 1;
        showLine(V, T);
        hold on
        plot(points, 0, "m*", "MarkerSize", 10);
        hold off
    case "2d"
        V = [0 0;
            1 0;
            0 1];
        T = [1 2 3];
        showTri(V, T);
        hold on
        plot(points(:,1), points(:,2), "m*", "MarkerSize", 10);
        hold off
    case "3d"
        V = [0 0 0;
            1 0 0;
            0 1 0;
            0 0 1];
        T = [1 2 3 4];
        showTetra(V, T);
        hold on
        plot3(points(:,1), points(:,2), points(:,3), "m*", "MarkerSize", 10);
        hold off
end
end

%% integral_ref
function [points, weights] = integral_ref(int_type)
switch int_type
    case "1d" % gauss-lengdre(4)
        points = [-0.8611363115940525752239465;
            -0.3399810435848562648026658;
            0.3399810435848562648026658;
            0.8611363115940525752239465];
        weights = [0.3478548451374538573730639, 0.6521451548625461426269361, 0.6521451548625461426269361, 0.3478548451374538573730639];
    case "2d" % Dunavant(7)
        points = [1/3, 1/3;
            0.470142064105115, 0.470142064105115;
            0.470142064105115, 0.059715871789770;
            0.059715871789770, 0.470142064105115;
            0.101286507323456, 0.101286507323456;
            0.101286507323456, 0.797426985353087;
            0.797426985353087, 0.101286507323456];
        weights = [0.1125, 0.066197076394253, 0.066197076394253, 0.066197076394253, 0.062969590272414, 0.062969590272414, 0.062969590272414];

        % points = [0.11270166537925829786104259255808, 0.1;
        %     0.11270166537925829786104259255808, 0.44364916731037085106947870372096;
        %     0.11270166537925829786104259255808, 0.78729833462074172434341789994505;
        %     0.5, 0.056350832689629148930521296279039;
        %     0.5, 0.25;
        %     0.5, 0.44364916731037085106947870372096;
        %     0.88729833462074170213895740744192, 0.01270166537925830792243875322356;
        %     0.88729833462074170213895740744192, 0.056350832689629148930521296279039;
        %     0.88729833462074170213895740744192, 0.1];
        % weights = [0.068464377671353521259689500766399, 0.10954300427416563401550320122624, 0.068464377671353521259689500766399, 0.061728395061728395061728395061728, 0.098765432098765432098765432098765, 0.061728395061728395061728395061728, 0.0086961161558069665494796751659123, 0.013913785849291147520001565851544, 0.0086961161558069665494796751659123];
    case "3d"
        points = [0.09273525035, 0.09273525035, 0.09273525035;
            0.72179424905, 0.09273525035, 0.09273525035;
            0.09273525035, 0.72179424905, 0.09273525035;
            0.09273525035, 0.09273525035, 0.72179424905;
            0.31088591930, 0.31088591930, 0.31088591930;
            0.06734224225, 0.31088591930, 0.31088591930;
            0.31088591930, 0.06734224225, 0.31088591930;
            0.31088591930, 0.31088591930, 0.06734224225;
            0.45449629590, 0.45449629590, 0.04550370415;
            0.45449629590, 0.04550370415, 0.45449629590;
            0.04550370415, 0.45449629590, 0.45449629590;
            0.45449629590, 0.04550370415, 0.04550370415;
            0.04550370415, 0.45449629590, 0.04550370415;
            0.04550370415, 0.04550370415, 0.45449629590];
        weights = [0.0122488405125, 0.0122488405125, 0.0122488405125, 0.0122488405125, 0.0187813209500, 0.0187813209500, 0.0187813209500, 0.0187813209500, 0.0070910034625, 0.0070910034625, 0.0070910034625, 0.0070910034625, 0.0070910034625, 0.0070910034625];

        % points = [0.25, 0.25, 0.25;
        %     0.319793627829630, 0.319793627829630, 0.319793627829630;
        %     0.040619116511110, 0.319793627829630, 0.319793627829630;
        %     0.319793627829630, 0.040619116511110, 0.319793627829630;
        %     0.319793627829630, 0.319793627829630, 0.040619116511110;
        %     0.091971078052723, 0.091971078052723, 0.091971078052723;
        %     0.724086765841831, 0.091971078052723, 0.091971078052723;
        %     0.091971078052723, 0.724086765841831, 0.091971078052723;
        %     0.091971078052723, 0.091971078052723, 0.724086765841831;
        %     0.443649167310371, 0.056350832689629, 0.056350832689629;
        %     0.056350832689629, 0.443649167310371, 0.056350832689629;
        %     0.056350832689629, 0.056350832689629, 0.443649167310371;
        %     0.056350832689629, 0.443649167310371, 0.443649167310371;
        %     0.443649167310371, 0.056350832689629, 0.443649167310371;
        %     0.443649167310371, 0.443649167310371, 0.056350832689629];
        % weights = [0.019753086419753, 0.011989513963170, 0.011989513963170, 0.011989513963170, 0.011989513963170, 0.011511367871045, 0.011511367871045, 0.011511367871045, 0.011511367871045, 0.008818342151675, 0.008818342151675, 0.008818342151675, 0.008818342151675, 0.008818342151675, 0.008818342151675];

        % points = [0.25, 0.25, 0.25;
        %     1/6, 1/6, 1/6;
        %     0.5, 1/6, 1/6;
        %     1/6, 0.5, 1/6;
        %     1/6, 1/6, 0.5];
        % weights = [-2/15, 3/40, 3/40, 3/40, 3/40];
    otherwise
        error("Invalid gauss-lengdre type.");
end
end

%% showLine
function showLine(V, T)
Np = size(V,1);
Ne = size(T,1);
figure("WindowStyle", "docked");
line(V, [0; 0], "Color", "k");
hold on
% node
plot(V, 0, "k.", "MarkerSize", 10);
text(V, [0; 0], int2str((1:Np)'), "FontSize", 10, "FontWeight", "bold", "Color", "k");
% elem
midpoint = 0.5*(V(1) + V(2));
text(midpoint, 0, int2str((1:Ne)'), "FontSize", 10, "FontWeight", "bold", "Color", "r", "HorizontalAlignment", "right");
hold off
axis("tight", "equal");
xlabel("X");
end

%% showTri
function showTri(V, T)
Np = size(V,1);
Ne = size(T,1);
Ned = 3;
edge = [1 2;
    2 3;
    3 1];
figure("WindowStyle", "docked");
h = patch("Faces", T, "Vertices", V);
set(h, "facecolor", "w", "edgecolor", "k");
hold on
% node
plot(V(:,1), V(:,2), "k.", "MarkerSize", 10);
text(V(:,1), V(:,2), int2str((1:Np)'), "FontSize", 10, "FontWeight", "bold", "Color", "k");
% elem
center = zeros(Ne,2);
for i = 1:Ne
    V = V(T(i,:),:);
    center(i,:) = mean(V);
end
text(center(:,1), center(:,2), int2str((1:Ne)'), "FontSize", 10, "FontWeight", "bold", "Color", "r", "HorizontalAlignment", "right");
% edge
midpoint = 0.5*(V(edge(:,1),:) + V(edge(:,2),:));
text(midpoint(:,1), midpoint(:,2), int2str((1:Ned)'), "FontSize", 10, "FontWeight", "bold", "Color", "b", "HorizontalAlignment", "right");
hold off
axis("tight", "equal");
xlabel("X");
ylabel("Y");
end

%% showTetra
function showTetra(V, T)
Np = size(V,1);
Ne = size(T,1);
Nf = 4;
Ned = 6;
tri = [4 3 2;
    1 3 4;
    4 2 1;
    1 2 3];
edge = [4 3;
    3 2;
    2 4;
    1 3;
    4 1;
    2 1];
figure("WindowStyle", "docked");
tetramesh(T, V, "FaceColor", "none");
hold on;
% node
plot3(V(:,1), V(:,2), V(:,3), "k.", "MarkerSize", 10);
text(V(:,1), V(:,2), V(:,3), num2str((1:Np)'), "FontSize", 10, "FontWeight", "bold", "Color", "k", "HorizontalAlignment", "right", "VerticalAlignment", "bottom");
% elem
centroid = zeros(Ne,3);
for e = 1:Ne
    E = V(T(e,:),:);
    centroid(e,:) = mean(E,1);
end
text(centroid(:,1), centroid(:,2), centroid(:,3), num2str((1:Ne)'), "FontSize", 10, "FontWeight", "bold", "Color", "r", "HorizontalAlignment", "right", "VerticalAlignment", "bottom");
% face
centroid = zeros(Nf,3);
for e = 1:Nf
    F = V(tri(e,:),:);
    centroid(e,:) = mean(F,1);
end
text(centroid(:,1), centroid(:,2), centroid(:,3), num2str((1:Nf)'), "FontSize", 10, "FontWeight", "bold", "Color", "g");
% edge
midpoint = 0.5*(V(edge(:,1),:) + V(edge(:,2),:));
text(midpoint(:,1), midpoint(:,2), midpoint(:,3), int2str((1:Ned)'), "FontSize", 10, "FontWeight", "bold", "Color", "b");
hold off;
axis("tight", "equal");
xlabel("X");
ylabel("Y");
zlabel("Z");
view(120, 30);
end